<template>
	<div class="counter my_flex_center">
		<!--<img @click="changeCount(-1)" src="../../assets/priceType/icon_left.png" />
		<input class="my_flex_center" v-model="obj_[key_]" :min="min" max="9999" maxlength="4" type="number" @input="changeValue()"></input>
		<img src="../../assets/priceType/icon_right.png" @click="changeCount(1)" />-->
		<!--{{obj_}}-->
		<!--{{obj_.product.qty}}-->
		<!--{{obj_.qty}}-->
		<el-input-number v-model="obj_[key_]" :min="obj_.minBuyQty" :max="obj_.product.qty" @change="changeCount" @input.native="changeValue($event)"></el-input-number>

	</div>
</template>
<script>
	export default {
		props: {
			originCount: {
				type: Number,
				default: 1
			},
			obj_: {
				type: Object,
				default: null
			},
			key_: {
				type: String,
				default: ''
			},
			min: {
				type: Number,
				default: 1
			},
			hascondition: {
				type: Number,
				default: 0
			}
		},
		data() {
			return {
				count: 1,
			}
		},
		mounted() {
			if(!this.obj_[this.key_]) {
				this.count = this.originCount;
			} else {
				this.count = this.obj_[this.key_];
			}
		},
		computed: {

		},
		methods: {
			changeCount(i) {
				//				if(parseInt(this.count) + i < this.min && i < 1) {
				//					return false;
				//				} else if(parseInt(this.count) + i > 999999) {
				//					return false;
				//				}
				//				if(!this.hascondition) {
				//					this.count = parseInt(this.count) + i;
				//					this.obj_[this.key_] = this.count;
				//				}
				console.log(this.obj_.qty);
				this.$emit('change', this.obj_, i, false);
			},
			changeValue(ev) {

				let before = this.obj_.qty;

				this.$nextTick(() => {

					var re = /^[0-9]+$/;
					if(this.obj_.qty == '') {
						this.obj_.qty = this.min;
					}

					let value = $(ev.target).val();

					if(re.test(value)) {
						
						if(value < this.min) {
							this.obj_.qty = this.min;
						}

						if(value > 999999) {
							this.obj_.qty = 999999;
						}
						console.log(this.obj_.qty)
						this.$emit('change', this.obj_, this.obj_.qty, true);
					} else {
						this.obj_.qty = this.obj_.qty.replace(/[^\d]/gi, '');
					}
				})

			}
		},
		watch: {
			'obj_.qty' (newvalue, oldvalue) {
				if(newvalue && newvalue.length == 5) {
					this.obj_[this.key_] = this.obj_[this.key_].splice(0, 3)
				}
				this.count = newvalue;
			}
		}
	}
</script>
<style lang="less">
	@import '../../less/base/base.less';
	.counter {}
</style>